package cn.huapu.power.energy.controller;

import cn.huapu.power.energy.bean.*;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.bean.vo.DropDownVo;
import cn.huapu.power.energy.bean.vo.PeaUserpowerfeeVo;
import cn.huapu.power.energy.bean.vo.Ranking5Vo;
import cn.huapu.power.energy.ex.ErrorCodesException;
import cn.huapu.power.energy.service.*;
import cn.huapu.power.energy.util.ValidateUtil;
import cn.huapu.power.energy.util.python.RunPython;
import cn.huapu.power.energy.vo.Grid;
import cn.huapu.power.energy.vo.Page;
import cn.huapu.power.energy.vo.Ret;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author jzx
 * @title: FeesController
 * @projectName energy
 * @description: 电费单项目相关接口
 * @date 2019-7-189:02
 */
@RestController
@RequestMapping("/fees")
public class FeesController {
    private Log log = LogFactory.getLog(FeesController.class);

    @Autowired
    private PeaRankService ankService;
    @Autowired
    private PeaFeeoptimizeService zeService;
    @Autowired
    private PeaUserpowerfeeService feeService;
    @Autowired
    private PeaIndustrytypetService petService;
    @Autowired
    private SysVoltageleveltService ltService;
    @Autowired
    private PeaFrequencyService cyService;
    @Autowired
    private PeaIndustrytypetService industrytypetService;
    @Autowired
    private SysClientService clientService;

    /**
     * 电费单查询
     *
     * @param clientId
     * @param page
     * @param rows
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-14 11:21
     */
    @RequestMapping(value = "/find/{clientId}", method = RequestMethod.GET)
    public Ret find(@PathVariable("clientId") Integer clientId,
                    @RequestParam(value = "page", required = false) Long page,
                    @RequestParam(value = "rows", required = false) Long rows) {
        Ret ret = new Ret();
        Page pa = new Page();
        if (page != null) {
            pa.setPage(page);
        }
        if (rows != null) {
            pa.setRows(rows);
        }
        PeaUserpowerfeeVo peaUserpowerfeeVo = new PeaUserpowerfeeVo();
        peaUserpowerfeeVo.setClientid(clientId);
        Grid<PeaUserpowerfeeVo> peaUserpowerfeeGrid = feeService.queryByPage(pa, peaUserpowerfeeVo);
        ret.OK(peaUserpowerfeeGrid);
        return ret;
    }


    /**
     * 电费单添加
     *
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-14 11:14
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Ret add(@RequestBody PeaUserpowerfee peaUserpowerfee) {
        Ret ret = new Ret();
        int insert = feeService.add(peaUserpowerfee);
        if (insert > 0) {
            ret.OK("电费单添加成功!", insert);
        } else {
            ret.OK("电费单添加失败!", insert);
        }
        return ret;
    }

    /**
     * 电费单添加
     *
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-14 11:14
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Ret edit(@RequestBody PeaUserpowerfee peaUserpowerfee) {
        Ret ret = new Ret();
        int update = feeService.edit(peaUserpowerfee);
        if (update > 0) {
            ret.OK("电费单修改成功!", update);
        } else {
            ret.OK("电费单修改失败!", update);
        }
        return ret;
    }




    /**
     * 电费单删除
     *
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-14 11:14
     */
    @RequestMapping(value = "/delete/{pkid}", method = RequestMethod.POST)
    public Ret delete(@PathVariable("pkid")  Integer pkid) {
        Ret ret = new Ret();
        int remove = feeService.remove(pkid);
        if (remove > 0) {
            ret.OK("电费单删除成功!", remove);
        } else {
            ret.OK("电费单删除失败!", remove);
        }
        return ret;
    }





    /**
     * 工业分类下拉
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-15 17:00
     */
    @RequestMapping(value = "/idstyTypeDrop", method = RequestMethod.GET)
    public Ret idstyTypeDrop() {
        Ret ret = new Ret();
        List<DropDownVo> dropDown=industrytypetService.idstyTypeDrop();
        ret.OK("查询下拉成功!",dropDown);
        return ret;
    }


    /**
     * 电压等级下拉
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-15 17:00
     */
    @RequestMapping(value = "/volLevelDrop", method = RequestMethod.GET)
    public Ret volLevelDrop() {
        Ret ret = new Ret();
        List<DropDownVo> dropDown=ltService.volLevelDrop();
        ret.OK("查询下拉成功!",dropDown);
        return ret;
    }


    /**
     * 公司详情
     * @param
     * @return cn.huapu.power.energy.vo.Ret
     * @throws
     * @date 2020-5-15 17:00
     */
    @RequestMapping(value = "/getClientById/{clientId}", method = RequestMethod.GET)
    public Ret getClientById(@PathVariable("clientId") Integer clientId) {
        Ret ret = new Ret();
        SysClient sysClient=clientService.get(clientId);
        ret.OK("查询成功!",sysClient);
        return ret;
    }








    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //分行业与不分行业排名
     * @Date 16:19 2019-7-18
     * @Param [da]
     **/
    @RequestMapping(value = "/ranking", method = RequestMethod.POST)
    public Ret ranking(@RequestBody DateParameterBo da) {
//        Map<String, String> syDate = DateUtils.getSyDate(DateUtils.getStringDateShort());
        Map<String, Object> map = ankService.ranking(da);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //用电评价总得分最近五次得分与排名
     * @Date 9:05 2019-7-19
     * @Param [da]
     **/
    @RequestMapping(value = "/ranking_5", method = RequestMethod.POST)
    public Ret ranking_5(@RequestBody DateParameterBo da) {
        List<Ranking5Vo> list = ankService.ranking_5(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //电费单数据采集
     * @Date 9:06 2019-7-19
     * @Param [da]
     **/
    @RequestMapping(value = "/feesCollection", method = RequestMethod.POST)
    public Ret feesCollection(@RequestBody PeaUserpowerfee bo) {
        Map<String, Object> map = feeService.feesCollection(bo);
        Ret r = new Ret();
        if (!ValidateUtil.mapIsNull(map)) {
            String pkid = String.valueOf(map.get("pkid"));
            RunPython.runPeapy(pkid);
            r.OK("采集成功");
            log.info("pkid=" + pkid + "***********************");
        } else {
            r.Error("采集失败", null);
        }
        return r;
    }

    @RequestMapping(value = "/testRunPy", method = RequestMethod.GET)
    public Ret feesCollection(String pkid) {
        Ret r = new Ret();
        RunPython.runPeapy(pkid);
        return r;
    }


    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //行业类别下拉接口
     * @Date 9:08 2019-7-19
     * @Param [da]
     **/
    @RequestMapping(value = "/industryDrop", method = RequestMethod.POST)
    public Ret industryDrop(@RequestBody DateParameterBo da) {
        List<PeaIndustrytypet> list = petService.industryDrop(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //电压下拉
     * @Date 9:10 2019-7-19
     * @Param [da]
     **/
    @RequestMapping(value = "/voLevelt", method = RequestMethod.POST)
    public Ret voLevelt() {
        List<SysVoltagelevelt> list = ltService.voLevelt();
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
     * @return
     * @Author jzx
     * @Description //电价优化模式
     * @Date 9:16 2019-7-20
     * @Param
     **/
    @RequestMapping(value = "/pricePattern", method = RequestMethod.POST)
    public Ret pricePattern(@RequestBody DateParameterBo da) {
        Map<String, Object> map = zeService.pricePattern(da);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //本月次月电量
     * @Date 9:38 2019-7-20
     * @Param [da]
     **/
    @RequestMapping(value = "/bycyPower", method = RequestMethod.POST)
    public Ret bycyPower(@RequestBody DateParameterBo da) {
        List<PeaFeeoptimize> list = zeService.bycyPower(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //(功率因数)
     * @Date 9:55 2019-7-20
     * @Param [da]
     **/
    @RequestMapping(value = "/baseInfo", method = RequestMethod.POST)
    public Ret baseInfo(@RequestBody DateParameterBo da) {
        Map<String, Object> map = feeService.baseInfo(da);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //销峰填谷
     * @Date 11:40 2019-7-20
     * @Param [da]
     **/
    @RequestMapping(value = "/xftgBaseInfo", method = RequestMethod.POST)
    public Ret xftgBaseInfo(@RequestBody DateParameterBo da) {
        Map<String, Object> map = zeService.xftgBaseInfo(da);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }


    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //均价得分排名分布图
     * @Date 9:17 2019-8-3
     * @Param [da]
     **/
    @RequestMapping(value = "/frequency", method = RequestMethod.POST)
    public Ret frequency(@RequestBody DateParameterBo da) {
        Map<String, Object> map = cyService.frequency(da);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //近期排名情况
     * @Date 10:35 2019-8-3
     * @Param [da]
     **/
    @RequestMapping(value = "/jqRanking", method = RequestMethod.POST)
    public Ret jqRanking(@RequestBody DateParameterBo da) {
        List<Ranking5Vo> list = ankService.jqRanking(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
     * @return cn.huapu.power.energy.vo.Ret
     * @Author jzx
     * @Description //word文件下载
     * @Date 13:55 2019-8-3
     * @Param [da]
     **/
    @RequestMapping(value = "/downlodeWord", method = RequestMethod.POST)
    public void downlodeWord(@RequestBody DateParameterBo da, HttpServletResponse response) throws IOException {
        InputStream wordFile = ankService.downlodeWord(da);

        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        // 下载文件能正常显示中文
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("fees", "UTF-8"));
        // 实现文件下载
        byte[] buffer = new byte[1024];
        FileInputStream fis = (FileInputStream) wordFile;
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            throw new ErrorCodesException("下载出错,请联系管理员", ErrorCodesException.FILE);
        }
    }

}
